﻿
function AjaxRequestResult(ControlId,DataUrl) {
   //开始请求数据
   var PageIndex=$("#"+ControlId).attr("PageIndex");
   $("#"+ControlId).attr("Condition",DataUrl);
   ResultPage(ControlId,PageIndex);
}

function ResultPage(ControlId,PageIndex) {
 
    ControlId=typeof(ControlId)=="string"?ControlId:$(ControlId).attr("Id");
   
    var Path=$("#"+ControlId).attr("Path");  //请求路径和方法
    var Condition=$("#"+ControlId).attr("Condition"); //检索条件
    var FieldSort=$("#"+ControlId).attr("FieldSort"); //排序条件
    var PageSize=$("#"+ControlId).attr("PageSize"); //每页多少条数据
    
    
    var dataParameter;
    
    dataParameter=typeof(Condition)!="undefined"? Condition:"";
    dataParameter+=typeof(FieldSort)!="undefined"? FieldSort:"";

    var DataUrl = "PageSize=" + PageSize + "&PageIndex=" + PageIndex + "&DateTime=" + new Date().getTime() + "&" + dataParameter;

    $.ajax({
        type: "get",
        url: Path,
        dataType: "json",
        data: DataUrl,
        cache: false,
        success: function (Msg) {
            window.top.CloseLoading();
            var Count = Msg.total, PageIndex = Msg.PageIndex, PageSize = Msg.PageSize, CountPage = Math.ceil(Count / PageSize), data = Msg.data, Center = "";
            $("#" + ControlId + " li:gt(0)").remove();  //清空出标头以外的数据
            $("#" + ControlId + " .Page").remove();  //清空分页
            $("#" + ControlId + " .PageNote").remove();  //移除分页注释
            $("#" + ControlId + " .NullMsg").remove(); //清空

            var ControlWidth = $("#" + ControlId).css("width");
            if (parseInt(ControlWidth) == 0) {
                $("#" + ControlId).css("width", "500px");
            }
            var ColumnNum = $("#" + ControlId + " li div").length;
            ControlWidth = parseInt(ControlWidth) == 0 ? 500 : parseFloat(ControlWidth) - ColumnNum;

            var TWidth = 0;
            $("#" + ControlId + " li div").each(function () {
                var Field = $(this).css("width");
                if ($(this).css("display") == "none") {
                    ColumnNum--;
                    return;
                }
                if (typeof (Field) == "undefined" || Field == "auto" || parseInt(Field) == 0) return;

                if (Field.indexOf("%") > 0) {
                    var Percent = parseInt(ControlWidth / parseFloat(Field));
                    TWidth += Percent;

                }
                if (Field.indexOf("px") > 0) {
                    TWidth += parseFloat(Field);

                }
                ColumnNum--;
            });
            var autoWidth = parseInt((ControlWidth - TWidth) / ColumnNum);

            $("#" + ControlId + " li div").each(function () {
                var Field = $(this).css("width");
                if (typeof (Field) == "undefined" || Field == "auto" || parseInt(Field) == 0)
                    $(this).css("width", autoWidth + "px");
                else
                    return;
            });

            if (Count == 0) {
                var NullDiv = "<div class='NullMsg' style=' width:100%; height:100px; color:#3366cc; font-weight:bold; line-height:100px; text-align:center;'></div>";
                $("#" + ControlId).append(NullDiv);
                return;  //数量等于空直接返回
            }

            for (var i = 0; i < data.length; i++) {
                Center += "<li>";

                var ArgList = ""; //记录属性

                $("#" + ControlId + " li div").each(function () {
                    var column = $(this).attr("column");  //获取绑定的列名

                    if (typeof (column) != "undefined" & column != null & column != "") {

                        var Value = eval("data[i]." + column);  //获取列名数据
                        var Css = $(this).attr("style");   //获取绑定控件样式
                        var Command = $(this).attr("command");  //获取命令

                        if (Value.toString() != "" && Value.toString() != null) {
                            ArgList += " " + column + "='" + ReplaceChar(Value) + "'";
                        }

                        if (typeof (Command) != "undefined") {

                            Center += "<div column='" + column + "' style='" + Css + "'>";
                            var CommandArray = Command.split(",");

                            for (var y = 0; y < CommandArray.length; y++) {
                                var CmdEvent = CommandArray[y].split(":")[0];
                                var CmdTitle = typeof (CommandArray[y].split(":")[1]) != "undefined" ? CommandArray[y].split(":")[1] : Value;
                                var CmdButImg = $(this).attr(CmdEvent + "ImageSrc");
                                if (typeof (CmdButImg) != "undefined") {
                                    Center += "<img src='" + CmdButImg + "' " + ArgList + " onclick='" + CmdEvent + "(this);' style='cursor:pointer;' title='" + CmdTitle + "'/>&nbsp;&nbsp;";
                                } else {
                                    Center += "&nbsp;<a href='javascript:void(0);' " + ArgList + " onclick='" + CmdEvent + "(this);'>" + CmdTitle + "</a>";
                                }
                            }
                            Center += "</div>";
                        } else {
                            Center += "<div column='" + column + "' style='" + Css + "'>" + Value + "</div>";
                        }
                    }
                });
                Center += "</li>";
            }


            //排序单击事件
            $("#" + ControlId + " li div").each(function () {
                var field = $(this).attr("allowSort");

                if (typeof field != "undefined") {
                    $(this).css("cursor", "pointer").attr("title", "单击可以排序");

                    $(this).unbind("click").bind("click", function () {
                        $(this).siblings().removeClass("OrderDesc").removeClass("OrderAsc");
                        var sortOrder = "Desc";
                        if ($(this).hasClass("OrderDesc")) {
                            sortOrder = "Asc";
                            $(this).removeClass("OrderDesc").addClass("OrderAsc");
                        } else if ($(this).hasClass("OrderAsc")) {
                            sortOrder = "Desc";
                            $(this).removeClass("OrderAsc").addClass("OrderDesc");
                        } else {
                            $(this).addClass("OrderDesc");
                        }
                        DataUrl = "&sortField=" + $(this).attr("column") + "&sortOrder=" + sortOrder;
                        $("#" + ControlId).attr("FieldSort", DataUrl);
                        ResultPage(ControlId, PageIndex);
                    });
                }
            });


            $("#" + ControlId + " ul").append(Center); //获取的结果直接保存到控件中

            if (typeof (PageSize) != "undefined") {


                //下面代码分页
                var Page = "<div class='Page' style=\"width: auto; float:right; margin-right:5px; margin-bottom:5px; height: 25px; padding-top: 5px;\">";

                var PageSizeGather = $("#" + ControlId).attr("PageSizeGather");
//                if (typeof (PageSizeGather) != "undefined") {
//                    var PageSizeArrayList = PageSizeGather.split(',');

//                    Page += "<div class='DropDown' tabindex='-1'  style='float:left;'>";
//                    Page += "<a class='DropDownValue' href='javascript:void(0);'>" + PageSize + "</a></br>";
//                    var marginTop = (PageSizeArrayList.length * 20) + 16;
//                    Page += "<div class='DropDownList' style='margin-top:" + (-marginTop) + "px;' ><ul>";
//                    for (var i = 0; i < PageSizeArrayList.length; i++) {
//                        var Cls = PageSize == PageSizeArrayList[i] ? "selectd" : "";
//                        Page += "<li class=" + Cls + ">" + PageSizeArrayList[i] + "</li>";
//                    }
//                    Page += "</ul></div>";
//                    Page += "</div>";
//                }
                //<a class='Current' style='width:40px;color:#cccccc;'>首页</a><a class='Current' onclick='ResultPage(" + ControlId + ",1)'  style='width:40px;'>首页</a>
                if (PageIndex == 1)
                    Page += "<a class='Current' style='width:50px;color:#cccccc;'>上一页</a>";
                else
                    Page += "<a onclick='ResultPage(" + ControlId + "," + (parseInt(PageIndex) - 1) + ")'  class='CurrentPage' style='width:50px;'>上一页</a>";

                if (PageIndex > 3) { Page += "<a   class='Current' onclick='ResultPage(" + ControlId + "," + (PageIndex - 3) + ")'>...</a>"; }

                for (var i = 1; i <= CountPage; i++) {
                    if (i > (parseInt(PageIndex)) - 3 && i < (parseInt(PageIndex)) + 3) {
                        Page += "<a onclick='ResultPage(" + ControlId + "," + i + ")' class='CurrentPage " + (i == (parseInt(PageIndex)) ? "ClickCurrentPage" : "") + "'>" + i + "</a>";
                    }
                }
                //<a class='Current' style='width:40px;color:#cccccc;'>尾页</a><a onclick='ResultPage(" + ControlId + "," + CountPage + ")' class='Current' style='width:40px;'>尾页</a>
                if (CountPage - PageIndex > 2) { Page += "<a   class='Current' onclick='ResultPage(" + ControlId + "," + (PageIndex + 3) + ")'>...</a>"; }

                if (PageIndex >= CountPage)
                    Page += "<a class='Current' style='width:50px;color:#cccccc;'>下一页</a>";
                else
                    Page += "<a onclick='ResultPage(" + ControlId + "," + (parseInt(PageIndex) + 1) + ")' class='CurrentPage' style='width:50px;'>下一页</a>";

                Page += " </div>";
                //                var PageNote = "<div Class='PageNote' style=' width:auto; float:right;margin-right:5px; margin-bottom:5px; height: 25px; padding-top: 7px;'>每页&nbsp;" + PageSize + "&nbsp;条,共&nbsp;" + Count + "&nbsp;条</div>";

                if (CountPage == 1) {
                } else {
                    $("#" + ControlId).append(Page);
//                    $(".DropDown").focus(function () {
//                        $(this).find(".DropDownList").css("display", "block");
//                    }).blur(function () {
//                        $(this).find(".DropDownList").css("display", "none");
//                    });

//                    $(".DropDownList li").click(function () {
//                        $(".DropDownValue").text($(this).text());
//                        PageSize = $(this).text();
//                        $("#" + ControlId).attr("PageSize", PageSize);
//                        ResultPage(ControlId, 1);
//                    });
                }
            }
        },
        error: function () {
            window.top.CloseLoading();
            window.top.AlertMessage('加载错误,请稍后再试！');
        },
        beforeSend: function () {
            window.top.Loading();
        },
        complete: function () {
            //ajaxComplete();
        }
    });
}

function End()
{
  //结束请求  
}

function ReplaceChar(obj) {
    var char = obj.toString().replace("'", "&apos;").replace("\"", "&quot;").replace(">", "&gt;").replace("<", "&lt;");
    return char;
}